package mvp.com.bbc.rcjavaretrofitdaggermvp.app;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import com.anye.greendao.gen.DaoMaster;
import com.anye.greendao.gen.DaoSession;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.concurrent.TimeUnit;

import mvp.com.bbc.rcjavaretrofitdaggermvp.BuildConfig;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.apiUrl.ApiUrl;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.SrictModeHelper;
import mvp.com.bbc.rcjavaretrofitdaggermvp.service.InitializationService;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by ${cbf} on 2017/11/24.
 */

public class BaseApplication extends Application {
    private SQLiteDatabase db;
    protected static Context context;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    public static BaseApplication instances;
    private DaoMaster.DevOpenHelper openHelper;
    protected static Retrofit retrofit;

    public static BaseApplication getInstances() {
        return instances;
    }

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
        //64k初始化
        MultiDex.install(this);
        context = getBaseContext();
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        //设置网络请求超时时间限制
        client.connectTimeout(5, TimeUnit.SECONDS);
        //调试模式检测错误
        SrictModeHelper.setPolicy(BuildConfig.DEBUG);
        StrictMode.VmPolicy.Builder builder1 = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder1.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder1.detectFileUriExposure();
        }
        retrofit = new Retrofit.Builder()
                .client(client.build())
                .baseUrl(ApiUrl.baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        //启动服务，把耗时的初始化都放在服务里
        InitializationService.startinitialization(this);
        setDatabase();
    }

    /**
     * 设置greedao
     */
    private void setDatabase() {
        openHelper = new DaoMaster.DevOpenHelper(this, "noteppps-db", null);
        db = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public static Context getContext() {
        return context;
    }

    public static Retrofit getRetrofit() {
        return retrofit;
    }
}
